<?php
namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;
class Netlog extends Base
{

    public function index(){
        //$map = $this->_search();

        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if(input('user_id')){
                $map['n.user_id'] = input('user_id');
            }
            if(input('username')){
                $map['n.username'] = input('username');
            }
            if(input('level')){
                $map['n.level'] = input('level');
            }
            if(input('time_start') && input('time_end')){
                $time_start = strtotime(input('time_start'));
                $time_end = strtotime(input('time_end'));
                $map['n.w_time'] = ['between',[$time_start,$time_end]];
            }
            $count = Db::name('netlog')->alias('n')->where($map)->count();

            $field = 'n.*';
            $list = Db::name('netlog')
                ->alias('n')
                ->field($field)
                ->where($map)
                ->order('n.id desc ')
                ->page($page,$limit)
                ->select();
            if($list){
                foreach($list as $k=>$v){
                    $list[$k]['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
                }
            }

            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
        }
        $level_arr = Db::name('user_level')->field('level_name,rank')->select();
        View::assign('level_arr',$level_arr);
        return View::fetch();
    }

    public function excel(){
        $page = input('page');
        $limit = input('limit')?:15;
        $map = [];
        if(input('user_id')){
            $map['n.user_id'] = input('user_id');
        }
        if(input('username')){
            $map['n.username'] = input('username');
        }
        if(input('level')){
            $map['n.level'] = input('level');
        }
        if(input('time_start') && input('time_end')){
            $time_start = strtotime(input('time_start'));
            $time_end = strtotime(input('time_end'));
            $map['n.w_time'] = ['between',[$time_start,$time_end]];
        }
        $count = Db::name('netlog')->alias('n')->where($map)->count();

        $field = 'n.*';
        $list = Db::name('netlog')
            ->alias('n')
            ->field($field)
            ->where($map)
            ->order('n.id desc ')
            ->page($page,$limit)
            ->select();
        if($list){
            foreach($list as $k=>$v){
                $list[$k]['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
            }
        }
        $title = ['ID','会员ID','会员编号','原推荐人','新推荐人','管理员id','操作时间'];
        $arr = [];
        foreach($list as $k=>$v){
            $v['w_time'] = date('Y-m-d H:i:s',$v['reg_time']);
            $list[$k] = $v;
            $arr = [
                $v['id'], $v['user_id'],$v['username'],$v['old'],$v['new'],$v['admin_id'],$v['w_time']
            ];
            $data[] = $arr;
        }
        return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data,'title'=>$title]);
        //dump($title);
        //dump($data);exit;
        $this->export_excel($title,$data);
    }
}
